package org.foreningsgatan.modular.resources;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.foreningsgatan.modular.common.logging.Log;
import org.foreningsgatan.modular.resources.xmlloader.Entry;
import org.foreningsgatan.modular.resources.xmlloader.Resources;
import org.foreningsgatan.modular.resources.xmlloader.XMLResourceUtil;

/**
 * 
 * Copyright (c) 2008 Joakim Back
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * @author Joakim Back
 *
 */
public class ResourceManager {
	private Map<String, ResourceHandler> mHandlers;

	private Map<String, Object> mResources;
	
	private Log mLog;

	private static String getExtension(String vPath) {
		if (vPath == null) return null;
		
		int vIndex = vPath.lastIndexOf(".");

		return (vIndex > -1) ? vPath.substring(vIndex + 1) : null;
	}

	protected ResourceManager() {
		mHandlers = new HashMap<String, ResourceHandler>();
		mResources = new HashMap<String, Object>();
		mLog = new Log(this.getClass());
	}

	public void setHandlers(Map<String, ResourceHandler> handlers) {
		mHandlers = handlers;
	}

	public void put(String pId, Object pResource) {
		mResources.put(pId, pResource);
	}

	public Object get(String pId) {
		if (!mResources.containsKey(pId)) {
			mLog.warning("Resource not found: '%s'", pId);
		}
		
		return mResources.get(pId);
	}

	public void read(InputStream pInputStream) throws Exception {
		Resources vResources = XMLResourceUtil.read(pInputStream);

		ResourceHandler vHandler;
		String vExtension;
		String vRelativePath;

		for (Entry vEntry : vResources.getEntries()) {
			vExtension = getExtension(vEntry.getPath());
			vHandler = mHandlers.get(vExtension);
			
			if (vExtension == null) {
				/* TODO: Warning */
				
				mLog.warning("Extension is NULL");
				
				continue;
			}

			if (vHandler != null) {
				vRelativePath = vResources.getBasePath() + vEntry.getPath();
				
				mResources.put(vEntry.getId(), vHandler.load(vRelativePath));
				
				mLog.debug("%s: %s", vEntry.getId(), mResources.get(vEntry.getId()));
			} else {
				mLog.warning("Could not find handler for '%s'", vExtension);
			}
		}
	}
	
	public void read(File pFile) throws Exception {
		read(new FileInputStream(pFile));
	}
}
